<?php
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | NewThink [ Think More,Think Better! ]
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | 版权所有：山西创泰网络科技有限公司
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// | Author: lk Date:2019/7/24 Time:15:19
// +‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

namespace app\common\service;

use think\Db;

class companyIndustryService
{
    private $data = [];
    private $field = [
        'gasInVol'     => ['TS' => 'rukouyanqiliang', 'TX' => 'SCRfanyingqiXAIGqianyandaoliuliang'],
        'gasOutVol'    => ['TS' => 'chukouyanqiliang', 'TX' => ''],
        'sO2InDen'     => ['TS' => 'rukouliunongdu', 'TX' => ''],
        'o2InPer'      => ['TS' => 'rukouO2nongdu', 'TX' => 'SCRfanyingqiXAIGqianyandaoO2nongdu'],
        'o2OutPer'     => ['TS' => 'chukouO2nongdu', 'TX' => 'SCRfanyingqichukouyanqiO2nongdu'],
        'gasInTemp'    => ['TS' => 'rukouyanwen', 'TX' => 'SCRfanyingqiXjinkouyanqiwendu1'],
        'gasOutTemp'   => ['TS' => 'chukouyanwen', 'TX' => 'SCRfanyingqichukouyanqiwendu'],
        'caDen'        => ['TS' => 'shihuishijiangyemidu', 'TX' => ''],
        'slurrySupply' => ['TS' => 'shihuishigongjiangliang', 'TX' => ''],
        'nOxInDen'     => ['TS' => '', 'TX' => 'SCRfanyingqiXAIGqianyandaoyanqiNOXnongdu'],
        'nOxOutDen'    => ['TS' => '', 'TX' => 'SCRfanyingqichukouyanqiNOXnongdu'],
        'nH3OutDen'    => ['TS' => '', 'TX' => 'SCRfanyingqichukouyanqiNH3nongdu'],
        'nH3InVol'     => ['TS' => '', 'TX' => 'anqiliuliang'],
        'ph'           => ['TS' => 'PHzhi', 'TX' => '']
    ];
    private $str;
    private $facilityInfo;
    
    /**
     * companyIndustryService1 constructor.
     * @param $startTime
     * @param $endTime
     * @param $facilityId
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function __construct($startTime, $endTime, $facilityId)
    {
        $this->facilityInfo = $this->facilityConversion($facilityId);
        $this->data         = $this->getData($startTime, $endTime);
        
    }
    
    /**
     * @param $startTime
     * @param $endTime
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    private function getData($startTime, $endTime)
    {
        $manager = new \MongoDB\Driver\Manager('mongodb://root:Ws4815115!@dds-bp1babc036f87d64-pub.mongodb.rds.aliyuncs.com:3717');
        
        $filter = [
            'nianyueri' => [
                '$gte' => $startTime,
                '$lte' => $endTime
            ]
        ];
        
        $projection = [
            'projection' => $this->getField()
        ];
        
        $query  = new \MongoDB\Driver\Query($filter, $projection);
        $cursor = $manager->executeQuery("huanxin.source_company_jin_neng_re_dian", $query);
        
        $result = [];
        foreach ($cursor as $document) {
            $result[] = (array)$document;
        }
        
        return $result;
    }
    
    /**
     * 根据工艺设施获取查询字段
     * @return array
     */
    private function getField()
    {
        switch ($this->facilityInfo['type']) {
            case 'TS':
                $this->str = '_' . $this->facilityInfo['machine_id'];
                break;
            case 'TX':
                $this->str = '_' . $this->facilityInfo['machine_id'] . '_' . $this->facilityInfo['facility'];
                break;
        }
        
        $column = array_filter(array_column($this->field, strtoupper($this->facilityInfo['type'])));
        $result = [];
        foreach ($column as $value) {
            $result[$value . $this->str] = 1;
        }
        
        return $result;
    }
    
    /**
     * 通过设施ID获取信息
     * @param $facilityId
     * @return array
     * @throws \Exception
     */
    private function facilityConversion($facilityId)
    {
        $sql      = "SELECT `name` FROM `basic_facility` WHERE id = ?";
        $facility = Db::query($sql, array($facilityId));
        if (empty($facility)) {
            throw new \Exception('未知设施');
        }
        $facilityName = $facility[0]['name'];
        
        $arr            = explode('-', $facilityName);
        $str            = substr($arr[1], strlen($arr[1]) - 1, 1);
        $facilityNumber = is_numeric($str) ? chr($str + 96) : false;
        
        $result = [
            'machine_id' => (int)substr($arr[1], 0, 1),//机组
            'facility'   => $facilityNumber,//治理设施,用于工况原始数据 a,b
            'type'       => $arr[0],//类型 TX,TS
        ];
        return $result;
    }
    
    /**
     * 获取循环泵运行组合
     * @param string $startTime
     * @param string $endTime
     * @return float|int
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function getPumpGroup($startTime = '', string $endTime = '')
    {
        $fieldName1 = 'no1xunhuanbengkaiguanzhuangtai' . $this->str;
        $fieldName2 = 'no2xunhuanbengkaiguanzhuangtai' . $this->str;
        $fieldName3 = 'no3xunhuanbengkaiguanzhuangtai' . $this->str;
        $fieldName4 = 'no4xunhuanbengkaiguanzhuangtai' . $this->str;
        
        $options = [
            'projection' => [
                '_id'       => 0,
                $fieldName1 => 1,
                $fieldName2 => 1,
                $fieldName3 => 1,
                $fieldName4 => 1
            ],
            'sort'       => [
                'insert_time' => -1
            ],
            'limit'      => 1
        ];
        $filter  = [
            'nianyueri' => [
                '$gte' => $startTime,
                '$lte' => $endTime
            ]
        ];
        
        $query   = new \MongoDB\Driver\Query($filter, $options);
        $manager = new \MongoDB\Driver\Manager(
            'mongodb://root:Ws4815115!@dds-bp1babc036f87d64-pub.mongodb.rds.aliyuncs.com:3717'
        );
        $cursor  = $manager->executeQuery("huanxin.source_company_jin_neng_re_dian", $query)->toArray();
        
        $data = json_decode(json_encode($cursor[0]), true);
        
        return array_sum($data);
    }
    
    /**
     * 获取石灰石粉品质
     * @return int
     */
    public function getCaQual(): int
    {
        return 95;
    }
    
    /**
     * @param $method
     * @param $args
     * @return float|int
     */
    public function __call($method, $args)
    {
        $type = $args[0] ?? 1;
        
        $param = lcfirst(substr($method, 3, strlen($method)));
        
        $field = $this->field[$param][$this->facilityInfo['type']] . $this->str;
        
        $column = array_column($this->data, $field);
        
        foreach ($column as &$v) {
            $v /= $type;
        }
        
        
        return array_sum($column) / count($column);
    }
}
